package com.ruoyi.web.controller.ycdj;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.TUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户接口")
@RestController
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    public TUserService tUserService;

    @Autowired
    public ISysUserService sysUserService;


    @ApiOperation("用户列表")
    @GetMapping("getlist")
    public AjaxResult getlist(@RequestParam(value="pageNum",defaultValue = "")Integer pageNum,
                              @RequestParam(value="pageSize",defaultValue = "")Integer pageSize
                              ){

        if(pageNum!=null){
            PageHelper.startPage(pageNum,pageSize);
            List<TUser>tUserList=tUserService.list();
            PageInfo<TUser>pageInfo=new PageInfo<>(tUserList);
            return AjaxResult.success(pageInfo);
        }else{
            List<TUser>tUserList=tUserService.list();
            return AjaxResult.success(tUserList);
        }
    }

    @ApiOperation("新增用户")
    @PostMapping("add")
    @Log(title = "新增用户", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody TUser tUser){
        QueryWrapper<TUser>tUserQueryWrapper=new QueryWrapper<>();
        tUserQueryWrapper.eq("loginname",tUser.getLoginname());
        tUserQueryWrapper.eq("username",tUser.getUsername());
        TUser tUser1=tUserService.getOne(tUserQueryWrapper);
        if(tUser1!=null){
            return AjaxResult.warn("用户已存在");
        }
        int sysuserid=Integer.parseInt(addSysuser(tUser).toString());
        if(sysuserid>0) {
            tUser.setSysuserid(sysuserid);
            return toAjax(tUserService.save(tUser));
        }else{
            return AjaxResult.error("新增失败");
        }
    }

    //同步增加系统用户
    public Long addSysuser(TUser tUser){
        SysUser sysUser=new SysUser();
        sysUser.setNickName(tUser.getLoginname());
        sysUser.setUserName(tUser.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(tUser.getPassword()));
        sysUser.setRemark("用户");
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUser.setDeptId(Long.parseLong(tUser.getDeptid().toString()));
        sysUser.setPhonenumber(tUser.getTelnum());

        if(sysUserService.insertUser(sysUser)>0){
            return sysUser.getUserId();
        }else{
            return 0L;
        }
    }

    public boolean editSysuser(TUser tUser){

        Long user_id=Long.parseLong(tUserService.getById(tUser.getId()).getSysuserid().toString()) ;
        SysUser sysUser=new SysUser();
        sysUser.setUserId(user_id);
        sysUser.setNickName(tUser.getLoginname());
        sysUser.setUserName(tUser.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(tUser.getPassword()));

        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUser.setDeptId(Long.parseLong(tUser.getDeptid().toString()));
        sysUser.setPhonenumber(tUser.getTelnum());

        if(sysUserService.updateUser(sysUser)>0){
            return true;
        }else{
            return false;
        }
    }


    //编辑
    @ApiOperation("编辑用户")
    @PostMapping("edit")
    @Log(title = "编辑用户", businessType = BusinessType.INSERT)
    public AjaxResult edit(@RequestBody TUser tUser){
        QueryWrapper<TUser>tUserQueryWrapper=new QueryWrapper<>();
        tUserQueryWrapper.eq("loginname",tUser.getLoginname());
        tUserQueryWrapper.eq("username",tUser.getUsername());
        TUser tUser1=tUserService.getOne(tUserQueryWrapper);
        if(tUser1!=null) {
            if (!tUser1.getId().equals(tUser.getId())) {
                return AjaxResult.warn("有重复，请更改别的名称");
            }
        }
        if(editSysuser(tUser)) {
            return toAjax(tUserService.saveOrUpdate(tUser));
        }else{
            return AjaxResult.error("编辑失败");
        }
    }

    //删除
    @ApiOperation("删除用户")
    @GetMapping("delete")
    @Log(title = "删除用户",businessType = BusinessType.DELETE)
    public AjaxResult delete(Integer id){
        TUser tuser=tUserService.getById(id);
        tuser.setStatus(0);
        if(tUserService.saveOrUpdate(tuser)){
            //修改系统用户
            SysUser sysUser=sysUserService.selectUserById(Long.parseLong(tuser.getSysuserid().toString()));
            sysUser.setStatus("0");
            return toAjax(sysUserService.updateUser(sysUser));
        }else{
            return AjaxResult.warn("删除用户失败");
        }
    }

    //获取用户信息
    @ApiOperation("获取用户信息")
    @GetMapping("getuserinfo")
    public AjaxResult getuserinfo(){
        SysUser sysUser=SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(sysUser);
    }

}
